package net.alpenviewer.app;
import java.awt.image.RenderedImage;
import java.io.File;

import javax.imageio.ImageIO;

import net.alpenviewer.map.ColorEncoder;
import net.alpenviewer.map.GradientColorEncoder;
import net.alpenviewer.map.MapBuilder;
import net.alpenviewer.map.SRTM;

/** */
public class ReliefGradientMaps {

	private static final String IMAGE_TYPE = "png";
	private static final File preprocessedDir = new File("srtm");
	private static final File slopeDir        = new File("relief/slope");
	static {
		if (!preprocessedDir.exists()) {
			preprocessedDir.mkdir();
		} else if (!preprocessedDir.isDirectory()) {
			throw new IllegalArgumentException(preprocessedDir.getName() + " is not a directory");
		}
		if (!slopeDir.exists()) {
			slopeDir.mkdirs();
		} else if (!slopeDir.isDirectory()) {
			throw new IllegalArgumentException(slopeDir.getName() + " is not a directory");
		}
	}

	public static void main(String[] args) throws Exception {

		long t = System.currentTimeMillis();

		final ColorEncoder gradientColor = new GradientColorEncoder();

		{ // legends
			final int w = 50; final int h = 120;

			final RenderedImage gradientLegend = gradientColor.getVerticalLegend(w, h);
			final File out = new File(slopeDir.getPath() + File.separator + "slopeLegend.png");
			ImageIO.write(gradientLegend, IMAGE_TYPE, out);			
		}


		{
			final File[] files = preprocessedDir.listFiles();
			for (File file : files) {

				if (file.isFile() && file.getName().toLowerCase().endsWith("." + SRTM.SUPPORTED_FILE_EXT)) {

					final SRTM srtm = new SRTM(file);
					System.out.println(srtm.toString());
					
					final RenderedImage img = MapBuilder.createGradientImage(srtm, gradientColor);

					final String slopeFilename = srtm.toString() + ".wgs84." + IMAGE_TYPE;
					final File out = new File(slopeDir.getPath() + File.separator + slopeFilename);
					ImageIO.write(img, IMAGE_TYPE, out);					
					
				}
			}
		}

		t = System.currentTimeMillis() - t;		
		System.out.println("processing elapsed " + t + " millis.");
	}

}
